<template>
  <div id="addQuestionView">
    <a-form :model="form" :style="{ width: '600px' }">
      <a-form-item field="title" label="题目标题">
        <a-input v-model="form.title" placeholder="please enter your post..." />
      </a-form-item>
      <a-form-item field="tags" label="题目标签">
        <a-input-tag
          v-model="form.tags"
          placeholder="Please Enter"
          :max-tag-count="3"
          allow-clear
        />
      </a-form-item>
      <a-form-item
        field="content"
        tooltip="Please enter username"
        label="题目内容"
      >
        <MdEditor :value="form.content" :handle-change="onContentChange" />
      </a-form-item>
      <a-form-item
        field="content"
        tooltip="Please enter username"
        label="题目答案"
      >
        <MdEditor :value="form.answer" :handle-change="onAnswerChange" />
      </a-form-item>
      <a-form-item field="judgeConfig" label="判题配置">
        <a-space direction="vertical" size="large">
          <a-form-item field="judgeConfig.timeLimit" label="时间限制">
            <a-input-number
              v-model="form.judgeConfig.timeLimit"
              :style="{ width: '320px' }"
              placeholder="Please Enter"
              mode="button"
              size="large"
              class="input-demo"
            />
          </a-form-item>
          <a-form-item field="judgeConfig.memoryLimit" label="内存限制">
            <a-input-number
              v-model="form.judgeConfig.memoryLimit"
              :style="{ width: '320px' }"
              placeholder="Please Enter"
              mode="button"
              size="large"
              class="input-demo"
            />
          </a-form-item>
          <a-form-item field="judgeConfig.stackLimit" label="堆栈限制">
            <a-input-number
              v-model="form.judgeConfig.stackLimit"
              :style="{ width: '320px' }"
              placeholder="Please Enter"
              mode="button"
              size="large"
              class="input-demo"
            />
          </a-form-item>
        </a-space>
      </a-form-item>
      <a-form-item
        v-for="(judgeCaseItem, index) of form.judgeCase"
        :key="index"
        label="测试用例配置"
      >
        <a-space direction="vertical" style="min-width: 480px">
          <a-form-item
            :field="`judgeCaseItem[${index}].input`"
            :label="`输入用例-${index}`"
          >
            <a-input
              v-model="judgeCaseItem.input"
              placeholder="please enter your post..."
            />
            <a-button
              @click="handleDelete(index)"
              :style="{ marginLeft: '10px' }"
              >删除
            </a-button>
          </a-form-item>
          <a-form-item
            :field="`judgeCaseItem[${index}].output`"
            :label="`输出用例-${index}`"
          >
            <a-input
              v-model="judgeCaseItem.output"
              placeholder="please enter your post..."
            />
            <a-button
              @click="handleDelete(index)"
              :style="{ marginLeft: '10px' }"
              >删除
            </a-button>
          </a-form-item>
        </a-space>
      </a-form-item>
      <a-form-item>
        <a-button @click="handleAdd">添加测试用例</a-button>
      </a-form-item>
      <a-form-item>
        <a-button html-type="submit" v-on:click="questionSubmit">提交</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import MdEditor from "@/components/MdEditor.vue";
import { QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute } from "vue-router";

const route = useRoute();
//如果页面地址包括update，视为更新页面
const updatePage = route.path.includes("update");
let form = ref({
  answer: "",
  content: "",
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
  judgeConfig: {
    memoryLimit: 1000,
    stackLimit: 1000,
    timeLimit: 1000,
  },
  tags: [],
  title: "",
});
onMounted(() => {
  loadData();
});
const loadData = async () => {
  const id = route.query.id as any;
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(id);
  if (res.code === 200) {
    form.value = res.data as any;
    //JSON转js对象
    if (!form.value.judgeCase) {
      form.value.judgeCase = [
        {
          input: "",
          output: "",
        },
      ];
    } else {
      form.value.judgeCase = JSON.parse(form.value.judgeCase as any);
    }
    if (!form.value.judgeConfig) {
      form.value.judgeConfig = {
        memoryLimit: 1000,
        stackLimit: 1000,
        timeLimit: 1000,
      };
    } else {
      form.value.judgeConfig = JSON.parse(form.value.judgeConfig as any);
    }
    if (!form.value.tags) {
      form.value.tags = [];
    } else {
      form.value.tags = JSON.parse(form.value.tags as any);
    }
  } else {
    message.error("加载失败");
  }
};

const handleAdd = () => {
  form.value.judgeCase.push({
    input: "",
    output: "",
  });
};
const handleDelete = (index: number) => {
  form.value.judgeCase.splice(index, 1);
};
const onContentChange = (v: string) => {
  form.value.content = v;
};
const onAnswerChange = (v: string) => {
  form.value.answer = v;
};

const questionSubmit = async () => {
  if (updatePage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(
      form.value
    );
    if (res.code === 200) {
      message.success("更新成功");
    } else {
      message.error("更新失败");
    }
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(
      form.value
    );
    if (res.code === 200) {
      message.success("添加成功");
    } else {
      message.error("添加失败");
    }
  }
};
</script>

<style scoped>
#addQuestionView {
}
</style>
